package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.EnableLoginAuth;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackOrderVo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;

    @PostMapping("/uploadTrack")
    public Result<Map<String, Object>> uploadTrack(MultipartFile file) {
        Map<String, Object> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    @EnableLoginAuth
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    @EnableLoginAuth
    @PostMapping("/findUserTrackPage/{page}/{limit}")
    public Result<IPage<TrackListVo>> findUserTrackPage(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            @RequestBody TrackInfoQuery query) {
        IPage<TrackListVo> pages = trackInfoService.findUserTrackPage(page, limit, query);
        return Result.ok(pages);
    }

    @GetMapping("/getTrackInfo/{trackId}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long trackId) {
        return Result.ok(trackInfoService.getById(trackId));
    }

    @EnableLoginAuth
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable Long trackId, @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackId, trackInfoVo);
        return Result.ok();
    }

    @EnableLoginAuth
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Long trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    @EnableLoginAuth(required = false)
    @GetMapping("/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
                                                             @PathVariable Integer page,
                                                             @PathVariable Integer limit) {
        Page<AlbumTrackListVo> albumTrackPage = this.trackInfoService.findAlbumTrackPage(albumId, page, limit);
        return Result.ok(albumTrackPage);
    }

    @GetMapping("/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = this.trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }

    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result<List<TrackOrderVo>> findUserTrackPaidList(@PathVariable Long trackId) {
        List<TrackOrderVo> trackOrderVos = this.trackInfoService.findUserTrackPaidList(trackId);
        return Result.ok(trackOrderVos);
    }

    @GetMapping("/findBuyTracks/{trackId}/{count}")
    public Result<List<TrackInfo>> findBuyTracks(@PathVariable Long trackId, @PathVariable Integer count) {
        List<TrackInfo> trackInfos = this.trackInfoService.findBuyTracks(trackId, count);
        return Result.ok(trackInfos);
    }
}

